import axios from 'axios'
import comment from './commnet'
import qs from 'qs'
import { useUserData } from '../stores/userData'
import {message} from 'ant-design-vue'
// axios封装
const request = axios.create({
    baseURL:`https://${comment.url}`,
})



// 请求拦截
request.interceptors.request.use(req=>{
    const loadHandler = useUserData();
    loadHandler.flag = true;
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    return req
})

request.interceptors.response.use(res=>{
    const loadHandler = useUserData();
    loadHandler.flag = false;
    if(res?.data?.code == 406 ||res.code == 406){
        return false;
    }
    return res
},err=>{
    const loadHandler = useUserData();
    loadHandler.flag = false;
    message.error('拉取失败',2)
})


export const POST = (url,data,params,headers)=>{

    return new Promise((resolve,reject)=>{
        request({
            url,
            method:"POST",
            data:urlData.includes(url)?data: qs.stringify(data),
            params:qs.stringify(params),
            headers, 
        }).then(result=>{
            resolve(result.data)
        }).catch(err=>{
            reject(err)
        })
    })
}

const urlData = [                   //不需要进行字符串转化的数据
    'http://yunguapp.cn/BackEndAjax/image',
    'http://yunguapp.cn/technicianAjax/Identity'
]

export default request